/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.stkent.polygondrawingutildemo.slice;

import com.stkent.polygondrawingutildemo.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Slider;
import ohos.agp.utils.Color;

import static java.lang.Math.max;

/**
 * MainAbilitySlice
 *
 * @since : 2021.5.24
 */
public class MainAbilitySlice extends AbilitySlice {
    private Float maxCornerRadius = 0f;
    private DemoView demoView;
    private Slider cornerRadiusSeekBar;
    private Slider rotationSeekBar;
    private Slider scaleSeekBar;

    @Override
    public void onStart(Intent intent) {
        getWindow().setStatusBarColor(Color.getIntColor("#2D3B9C"));
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        maxCornerRadius = 352f;
        initializeButtons();
        initializeSeekBars();
        initializeDemoView();
    }

    private void initializeButtons() {
        findComponentById(ResourceTable.Id_increase_side_count_button)
                .setClickedListener(component -> demoView.setNumberOfSides(demoView.getNumberOfSides() + 1));

        findComponentById(ResourceTable.Id_decrease_side_count_button)
                .setClickedListener(component -> demoView.setNumberOfSides(max(3, demoView.getNumberOfSides() - 1)));
    }

    private void initializeSeekBars() {
        if (findComponentById(ResourceTable.Id_corner_radius_seek_bar) instanceof Slider) {
            cornerRadiusSeekBar = (Slider) findComponentById(ResourceTable.Id_corner_radius_seek_bar);
        }
        if (findComponentById(ResourceTable.Id_rotation_seek_bar) instanceof Slider) {
            rotationSeekBar = (Slider) findComponentById(ResourceTable.Id_rotation_seek_bar);
        }
        if (findComponentById(ResourceTable.Id_scale_seek_bar) instanceof Slider) {
            scaleSeekBar = (Slider) findComponentById(ResourceTable.Id_scale_seek_bar);
        }
        cornerRadiusSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i1, boolean b1) {
                updateCornerRadius((float) i1);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
        rotationSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i1, boolean b1) {
                updateRotation((float) i1);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
        scaleSeekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i1, boolean b1) {
                updateScale((float) i1);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
    }

    private void initializeDemoView() {
        if (findComponentById(ResourceTable.Id_demo_view) instanceof DemoView) {
            demoView = (DemoView) findComponentById(ResourceTable.Id_demo_view);
        }
        updateCornerRadius((float) cornerRadiusSeekBar.getProgress());
        updateRotation((float) rotationSeekBar.getProgress());
        updateScale((float) scaleSeekBar.getProgress());
    }

    private void updateCornerRadius(Float cornerRadius) {
        demoView.setCornerRadius(cornerRadius / cornerRadiusSeekBar.getMax() * maxCornerRadius);
    }

    private void updateRotation(Float rotation) {
        demoView.setPolygonRotation(rotation * 360 / rotationSeekBar.getMax());
    }

    private void updateScale(Float scale) {
        demoView.setScale(scale / scaleSeekBar.getMax());
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
